package com.bj58.ecdata.dashboard.controllers;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.bj58.ecdata.dashboard.databll.IncomeBLL;
import com.bj58.ecdata.dashboard.databll.IncomeKPIBLL;
import com.bj58.ecdata.dashboard.utils.DateUtil;
import com.bj58.ecdata.dashboard.utils.MyServletRequestUtils;
import com.bj58.wf.mvc.ActionResult;
import com.bj58.wf.mvc.MvcController;
import com.bj58.wf.mvc.annotation.POST;
import com.bj58.wf.mvc.annotation.Path;

@Path("/income/ajax")
public class IncomeAjaxController extends MvcController {

	/**
	 * 城市-产品类型[精准，置顶，智能]数据
	 * 
	 * @return
	 */
	@Path("/cityProduct")
	@POST
	public ActionResult getCityProData() {
		try {
			HttpServletRequest request = beat.getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			String cityName = MyServletRequestUtils.getStringParameter(request, "cityName", "北京");
			String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "2014-01-01");

			String dataType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");

			String data = "";
			if (StringUtils.equalsIgnoreCase(dataType, "chart")) {
				data = IncomeBLL.getCityProJson(dateGroup, stat_type, businessName, cityName);
			} else if (StringUtils.equalsIgnoreCase(dataType, "table")) {
				data = IncomeBLL.getCityProTableJson(dateGroup, stat_type, businessName, dateVal);
			}
			// log.info(businessName+"--"+cityName+"\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			return ViewResult.view("{}");
		}
	}

	/**
	 * 类别-产品类型[精准，置顶，智能]数据
	 * 
	 * @return
	 */
	@Path("/cateProduct")
	@POST
	public ActionResult getCateProData() {
		try {
			HttpServletRequest request = beat.getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			String cateName = MyServletRequestUtils.getStringParameter(request, "cateName", "");
			String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "2014-01-01");

			String dataType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");

			// EnumCate cateEnum = EnumCate.valueOf(businessName);
			String data = "";
			if (StringUtils.equalsIgnoreCase(dataType, "chart")) {
				data = IncomeBLL.getCateProJson(dateGroup, stat_type, businessName, cateName);
			} else if (StringUtils.equalsIgnoreCase(dataType, "table")) {
				data = IncomeBLL.getCateProTableJson(dateGroup, stat_type, businessName, dateVal);
			}
			// log.info(businessName+"--"+cateName+"\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			return ViewResult.view("{}");
		}
	}

	/**
	 * 获取某个产品类型精准/置顶/智能的 两个城市对比数据
	 * 
	 * @return
	 */
	@Path("/cityCmp")
	@POST
	public ActionResult getCityCmpData() {
		try {
			HttpServletRequest request = beat.getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			// 产品类型，精准/置顶/智能
			String proType = MyServletRequestUtils.getStringParameter(request, "proType", "jingzhun");

			String cityName1 = MyServletRequestUtils.getStringParameter(request, "city1", "北京");
			String cityName2 = MyServletRequestUtils.getStringParameter(request, "city2", "天津");

			// EnumCate cateEnum = EnumCate.valueOf(businessName);
			String data = IncomeBLL.getCityCmpJson(dateGroup, stat_type, businessName, proType,
					StringUtils.strip(cityName1), StringUtils.strip(cityName2));
			// log.info(businessName+"--"+cityName1+"-"+cityName2+"--"+proType+"\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}

	/**
	 * 获取某个产品类型精准/置顶/智能的 两个类别对比数据
	 * 
	 * @return
	 */
	@Path("/cateCmp")
	@POST
	public ActionResult getCateCmpData() {
		try {
			HttpServletRequest request = beat.getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			// 产品类型，精准/置顶/智能
			String proType = MyServletRequestUtils.getStringParameter(request, "proType", "jingzhun");

			String cateName1 = MyServletRequestUtils.getStringParameter(request, "cate1", "");
			String cateName2 = MyServletRequestUtils.getStringParameter(request, "cate2", "");

			// EnumCate cateEnum = EnumCate.valueOf(businessName);
			String data = IncomeBLL.getCateCmpJson(dateGroup, stat_type, businessName, proType,
					StringUtils.strip(cateName1), StringUtils.strip(cateName2));
			// log.info(businessName+"--"+cateName1+"-"+cateName2+"--"+proType+"\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}

	/**
	 * 获取城市对比 雷达图
	 * 
	 * @return
	 */
	@Path("/cityRadar")
	@POST
	public ActionResult getCityRadarData() {
		try {
			HttpServletRequest request = beat.getRequest();
			// String dateGroup =
			// MyServletRequestUtils.getStringParameter(request, "dateGroup",
			// "stat_date");
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			String is_vip = MyServletRequestUtils.getStringParameter(request, "is_vip", "");
			String citys_radar = MyServletRequestUtils.getStringParameter(request, "citys_radar", "");
			String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "");

			String dataType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");

			String data = "";
			if (StringUtils.equalsIgnoreCase(dataType, "chart")) {
				data = IncomeBLL.getCityRadarJson(stat_type, dateVal, businessName, citys_radar, is_vip);
			} else if (StringUtils.equalsIgnoreCase(dataType, "table")) {
				data = IncomeBLL.getCityRadarTableJson(stat_type, dateVal, businessName, is_vip);
			}
			// log.info(businessName+"--"+citys_radar+"\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}

	/**
	 * 获取类别对比 雷达图
	 * 
	 * @return
	 */
	@Path("/cateRadar")
	@POST
	public ActionResult getCateRadarData() {
		try {
			HttpServletRequest request = beat.getRequest();
			// String dateGroup =
			// MyServletRequestUtils.getStringParameter(request, "dateGroup",
			// "stat_date");
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			String is_vip = MyServletRequestUtils.getStringParameter(request, "is_vip", "");
			String cates_radar = MyServletRequestUtils.getStringParameter(request, "cates_radar", "");
			String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "");

			String dataType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");

			String data = "";
			if (StringUtils.equalsIgnoreCase(dataType, "chart")) {
				data = IncomeBLL.getCateRadarJson(stat_type, dateVal, businessName, cates_radar, is_vip);
			} else if (StringUtils.equalsIgnoreCase(dataType, "table")) {
				data = IncomeBLL.getCateRadarTableJson(stat_type, dateVal, businessName, is_vip);
			}
			// log.info(businessName+"--"+cates_radar+"\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}

	/**
	 * 获取城市-消耗周期数据
	 * 
	 * @return
	 */
	@Path("/cityConsume")
	@POST
	public ActionResult getCityConsumeData() {
		try {
			HttpServletRequest request = beat.getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "");
			String statType = MyServletRequestUtils.getStringParameter(request, "statType", "");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			String consume_citys = MyServletRequestUtils.getStringParameter(request, "consume_citys", "北京,上海");
			String index1 = MyServletRequestUtils.getStringParameter(request, "index1", "consume_cycle");
			String index2 = MyServletRequestUtils.getStringParameter(request, "index2", "consume");

			String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "2014-01-01");
			String dataType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");

			String data = "";
			if (StringUtils.equalsIgnoreCase(dataType, "chart")) {
				data = IncomeBLL.getCityConsumeJson(dateGroup,statType, businessName, consume_citys, index1, index2);
			} else if (StringUtils.equalsIgnoreCase(dataType, "table")) {
				data = IncomeBLL.getCityConsumeTableJson(dateGroup,statType, businessName, dateVal);
			}
			// log.info(businessName+"--"+consume_citys+"--"+index1+"--"+index2+"\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			return ViewResult.view("{}");
		}
	}

	/**
	 * 获取类别-消耗周期数据
	 * 
	 * @return
	 */
	@Path("/cateConsume")
	@POST
	public ActionResult getCateConsumeData() {
		try {
			HttpServletRequest request = beat.getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String statType = MyServletRequestUtils.getStringParameter(request, "statType", "");

			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			String consume_citys = MyServletRequestUtils.getStringParameter(request, "consume_cates", "");
			String index1 = MyServletRequestUtils.getStringParameter(request, "index1", "consume_cycle");
			String index2 = MyServletRequestUtils.getStringParameter(request, "index2", "consume");

			String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "2014-01-01");
			String dataType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");

			String data = "";
			if (StringUtils.equalsIgnoreCase(dataType, "chart")) {
				data = IncomeBLL.getCateConsumeJson(dateGroup,statType, businessName, consume_citys, index1, index2);
			} else if (StringUtils.equalsIgnoreCase(dataType, "table")) {
				data = IncomeBLL.getCateConsumeTableJson(dateGroup,statType, businessName, dateVal);
			}
			// log.info(businessName+"--"+consume_citys+"--"+index1+"--"+index2+"\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			return ViewResult.view("{}");
		}
	}

	/**
	 * 分平台-产品类型[精准，置顶，智能]数据
	 * 
	 * @return
	 */
	@Path("/platform")
	@POST
	public ActionResult getPlatformData() {
		try {
			HttpServletRequest request = beat.getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			String product = MyServletRequestUtils.getStringParameter(request, "product", "all");
			String index = MyServletRequestUtils.getStringParameter(request, "index", "cost");
			String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "2014-01-01");

			String dataType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");

			String data = "";
			if (StringUtils.equalsIgnoreCase(dataType, "chart")) {
				data = IncomeBLL.getPlatformJson(dateGroup, stat_type, index, businessName, product);
			} else if (StringUtils.equalsIgnoreCase(dataType, "table")) {
				data = IncomeBLL.getPlatformTableJson(dateGroup, stat_type, dateVal, businessName, product);
			}
			// log.info(businessName+"--"+product+"\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			return ViewResult.view("{}");
		}
	}

	@Path("/getKpiByBusiness")
	@POST
	public ActionResult getKpiByBusiness() {
		String type_s = beat.getRequest().getParameter("type");
		if (StringUtils.isBlank(type_s)
				|| (!StringUtils.equalsIgnoreCase(type_s, "month") && !StringUtils.equalsIgnoreCase(type_s, "quarter"))) {
			return ViewResult.jsonView("{}");
		}
		try {
			String json = IncomeKPIBLL.getBusinessJson(type_s);
//			log.info(json);
			return ViewResult.jsonView(json);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.jsonView("{}");
		}
	}

	@Path("/getKpiByBusinessTrend")
	@POST
	public ActionResult getKpiByBusiness_trend() {
		String type_s = beat.getRequest().getParameter("type");
		String busiType = beat.getRequest().getParameter("busiType");

		if (StringUtils.isBlank(type_s)
				|| (!StringUtils.equalsIgnoreCase(type_s, "month") && !StringUtils.equalsIgnoreCase(type_s, "day"))) {
			return ViewResult.jsonView("{}");
		}
		try {
			String json = IncomeKPIBLL.getBusinessJsonTrend(type_s, busiType);

			return ViewResult.jsonView(json);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.jsonView("{}");
		}
	}

	@Path("/getKpiByOnline")
	@POST
	public ActionResult getKpiByOnline() {
		String type_s = beat.getRequest().getParameter("type");
		if (StringUtils.isBlank(type_s)
				|| (!StringUtils.equalsIgnoreCase(type_s, "month") && !StringUtils.equalsIgnoreCase(type_s, "quarter"))) {
			return ViewResult.jsonView("{}");
		}
		try {
			String json = IncomeKPIBLL.getOnlineJson(type_s);

			return ViewResult.jsonView(json);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.jsonView("{}");
		}
	}

	@Path("/getKpiByOnlineTrend")
	@POST
	public ActionResult getKpiByOnline_trend() {
		String type_s = beat.getRequest().getParameter("type");
		if (StringUtils.isBlank(type_s)
				|| (!StringUtils.equalsIgnoreCase(type_s, "month") && !StringUtils.equalsIgnoreCase(type_s, "day"))) {
			return ViewResult.jsonView("{}");
		}
		try {
			String json = IncomeKPIBLL.getOnlineJsonTrend(type_s);

			return ViewResult.jsonView(json);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.jsonView("{}");
		}
	}

	@Path("/getKpiBusinessup")
	@POST
	public ActionResult getKpiBusinessup() {
		String type_s = beat.getRequest().getParameter("type");
		String time = beat.getRequest().getParameter("time");
		if (StringUtils.isBlank(type_s)) {
			type_s = "month";
		}
		if (StringUtils.isBlank(time)) {
			time = DateUtil.getCurMonth();
		}
		try {
			String json = IncomeKPIBLL.getBusinessUpJson(type_s, time);

			return ViewResult.jsonView(json);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.jsonView("{}");
		}
	}

}
